<template>
	<view>
		<van-search
		  placeholder="车牌"
		  use-action-slot
		  @change="onChange"
		  @search="onSearch"
		>
		  <view slot="action" @tap="addNewBus">新增车辆</view>
		</van-search>
		
		<van-cell-group title="车辆列表">
			<!-- <van-cell v-for="(item, index) in buses" :key="index" :title="item.brand" :label="item.chepai">
				<view style="display: flex;flex-flow: row;justify-content: flex-end;">
					<van-button :data-item="item" :color="primaryColor" size="mini" @click="onSelectBus">选择</van-button>
					<view style="margin-left: 10px;"></view>
					<van-button :data-item="item" :color="primaryColor" size="mini" @click="onGotoBus" plain>详情</van-button>
				</view>
			</van-cell> -->
			<m-busselect-card v-for="(item, index) in buses" :key="index" @select="onGotoBus" :item="item" @click="onSelectBus"/>
			<!-- <m-busselect-card v-if="onlyShow" v-for="(item, index) in buses" :key="index" @select="onGotoBus" :item="item" @click="onGotoBus"/> -->
		</van-cell-group>
		
		<van-dialog id="van-dialog" />
		<van-toast id="van-toast" />
	</view>
</template>

<script>
	import utils from '@/common/utils.js'
	import Dialog from '@/wxcomponents/dialog/dialog'
	import Toast from '@/wxcomponents/toast/toast'
	export default {
		data() {
			return {
				primaryColor: getApp().globalData.primaryColor,
				buses:[],
				backIndex:0,
				pageObj:{
					page:1,
					pagesize:15
				},
				onlyShow:0,
				orderId: null
			};
		},
		async onLoad(options){
			console.log(options)
			this.orderId = options.orderid
			if(options.os == 1){
				this.onlyShow = 1
			}else{
				this.backIndex = Number(options.backindex)
			}
		},
		async onShow() {
			await this.resetPage()
		},
		async onReachBottom() {
			this.pageObj.page++
			await this.getCars()
		},
		methods:{
			onChange(){
				
			},
			async resetPage(){
				this.pageObj.page = 1
				this.buses = []
				await this.getCars()
			},
			onSearch(event){
				const str = event.detail
				this.buses = this.buses.filter(o => o.chepai.includes(str))
			},
			async getCars(){
				try{
					let reqData = {token: uni.getStorageSync('userinfo').token, ...this.pageObj}
					if(!this.onlyShow){
						reqData.filter = { orderId: this.orderId }
					}
					const rsp = await utils.req('/car/getCars', reqData)
					this.buses = [...this.buses,...rsp.list]
				}catch(e){
					//TODO handle the exception
				}
			},
			addNewBus(){
				uni.navigateTo({
					url:'../mbus-add/mbus-add'
				})
			},
			onGotoBus(event){
				const enData = encodeURIComponent(JSON.stringify(event))
				uni.navigateTo({
					url:`../mbus-detail/mbus-detail?data=${enData}`
				})
			},
			onSelectBus(event){
				if(this.onlyShow){
					return
				}
				const sel = event
				const pages = getCurrentPages()
				const prePage = pages[pages.length - 2]
				const items = prePage.$vm.routeItems
				let routeData = items.filter(o => o.backIndex == this.backIndex)[0]
				routeData.bus = {
					carId: sel.carId,
					number: sel.chepai
				}
				
				prePage.$vm.routeItems.splice(this.backIndex, 1, routeData)
				
				uni.navigateBack({
					delta:1
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
